<template lang="html">
    <div class="container" v-loading="loading">
        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input class="form_input" v-model="searchText" placeholder="请输入商品名称或编码"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                  <el-button type="primary" size="small" @click="onAddtion">新增</el-button>
                  <el-button type="danger" :disabled="isDisabled" size="small" @click="onBatch">批量删除</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="tables">
          <el-table :data="tableData" @selection-change="handleSelectionChange" :row-class-name="rowStyle" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column prop="date" label="主图" align="center" width="100">
                  <template slot-scope="scope">
                    <img style="width:80px;display:block" :src="scope.row.picUrl" alt="">
                  </template>
                </el-table-column>
                <el-table-column prop="commodityName" label="名称" align="center" width="180">
                  <template slot-scope="scope">
                    <p>{{scope.row.commodityName}}</p>
                    <p>编码：{{scope.row.commodityNumber}}</p>
                  </template>
                </el-table-column>
                <el-table-column prop="typeName" label="类型" align="center"></el-table-column>
                <el-table-column prop="styleName" label="款式" align="center"></el-table-column>
                <el-table-column prop="name" label="颜色" align="center" width="180">
                  <template slot-scope="scope">
                    <div class="colors">
                      <div class="clr" v-for="item in scope.row.colors" :key="item.id">
                        <p :style="{background:item.colorRgb}"></p>
                        <span>{{item.colorName}}</span>
                      </div>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="价格（元）" align="center">
                  <template slot-scope="scope">
                    <div class="" v-if="scope.row.minPrice && scope.row.maxPrice">
                      {{scope.row.minPrice.minPrice}}~{{scope.row.maxPrice.maxPrice}}
                    </div>
                  </template>
                </el-table-column>
                <el-table-column prop="name" label="状态" align="center" width="80">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="200">
                    <template slot-scope="scope">
                      <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                      <el-button type="danger" size="mini" @click="fDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>



    </div>
</template>

<script>
export default {
  data(){
    return {
      loading: false,
      searchText: '',
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      tableData: [],
      goType:null,
      currentData: []
    }
  },
  computed:{
    isDisabled:function() {
      return this.currentData.length>0?false:true;
    }
  },
  created(){
    this.selectCommodityPage();
  },
  methods:{
    selectCommodityPage(){
      this.loading = true;
      this.$http.selectCommodityPage({
        query: this.searchText,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      }).then(res=>{
        this.loading = false;
        this.tableData = res.data.rows;
        this.totalPage = res.data.total;
        this.pageSize = res.data.pageSize;
      });
    },
    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(JSON.stringify(row) == JSON.stringify(this.currentData[i])){
          return 'row_style';
        }
      }
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    onBatch(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('您确定要删除这些商品吗？').then(res=>{
        return this.$http.deleteCommoditys({ids:aId.join(',')})
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success('删除成功');
          this.selectCommodityPage();
        }else{
          this.$message.error(res.data.message);
        }
      }).catch(e=>0);
    },
    fDelete(e){
      this.$confirm('您确定要删除此商品吗？').then(res=>{
         this.$http.deleteCommodity({id:e.id}).then(res=>{
             if(res.data.code == 0){
               this.$message.success('删除成功');
               this.selectCommodityPage();
             }else{
               this.$message.error(res.data.message);
             }
         })
      }).catch(e=>0);
    },
    onSwitch(e){
      this.$http.closeCommodity({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    onAddtion(){
      this.goType = 1;
      this.$router.push({ path: '/commodity/goods/add_good',query:{title: '新增商品'}})
    },
    fEdit(e){
      this.goType = 2;
      this.$router.push({ path: '/commodity/goods/add_good',query:{id: e.id,title: '编辑商品'}})
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectCommodityPage();
    },
  },
  beforeRouteLeave(to, from , next){
    if(this.goType == 1 && to.path == '/commodity/goods/add_good'){
      to.meta.title = '新增商品';
    }else if(this.goType == 2 && to.path == '/commodity/goods/add_good'){
      to.meta.title = '编辑商品';
    }
    next();
  }

}
</script>

<style lang="css" scoped>
/deep/.el-table .row_style{
  background-color: #f8f8f8 !important;
}
.colors{
  display: flex;
  flex-wrap: wrap;
}
.colors .clr{
  display: flex;
  width: 50%;
  align-items: center;
  justify-content: center;
}
.colors .clr p{
  width:24px;
  height:16px;
  margin-right: 10px
}
</style>
